<?php

namespace App\Http\Controllers\Admins;

use App\Http\Controllers\AdminController;
use App\Http\Controllers\Controller;
use App\Models\AdminMenu;
use App\Models\AdminUser;
use App\Models\Coupon;
use App\Models\User;
use App\Services\WechatApi;
use Gregwar\Captcha\CaptchaBuilder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;

class Home extends AdminController
{
    public function test(Request $request)
    {

    }

    /**
     * 后台登录图形验证码
     * @param CaptchaBuilder $builder
     * @return mixed
     */
    public function captcha(CaptchaBuilder $builder)
    {
        $builder->build(140,40);

        Session::put('captcha',$builder->getPhrase());

        return $this->ReturnSuccessInfo($builder->inline());
    }

    /**
     * 后台登录
     * @param Request $request
     * @return mixed|\Symfony\Component\HttpFoundation\ParameterBag|null
     */
    public function login(Request $request)
    {
        $post = $request->all();

        $validator = Validator::make($post,[
            'username'  => 'required',
            'password'  => 'required',
            'vercode'   => 'required',
        ]);

        if ($validator->fails()){
            return $this->ReturnFailed(1,$validator->errors());
        }

        if (strtolower(Session::get('captcha')) != strtolower($post['vercode'])){
            return $this->ReturnErrorInfo(2,[strtolower(Session::get('captcha'))]);
        }

        if (! $token = $this->auth()->attempt($request->only(['username','password']))){

            return $this->ReturnErrorInfo(1);
        }

        return $this->ReturnSuccessInfo([
            'username'      => $request->username,
            'access_token'  => $token,
            'token_type'    => 'bearer',
            'expires_in'    => $this->auth()->factory()->getTTL() * 60,
        ]);
    }

    /**
     * 刷新token
     * @return mixed|\Symfony\Component\HttpFoundation\ParameterBag|null
     */
    public function refresh()
    {
        return $this->ReturnSuccessInfo([
            'token' =>$this->auth()->resresh(),
        ]);
    }

    /**
     * 导航栏
     * @return mixed
     */
    public function menu()
    {
        $menu = AdminMenu::orderBy('sort','desc')->get()->toArray();

        $data = [];
        foreach ($menu as $key=>$item){

            if ($item['parent']){

                $data[$item['parent']]['list'][] = $item;
            }else{
                $item['list'] = [];
                $data[$item['id']] = $item;

            }
        }

        return $this->ReturnSuccessInfo($data);
    }

    public function password(Request $request)
    {
        $validator = Validator::make($post = $request->all(),[
            'old_password'  => 'required|password:admin',
            'password'      => 'required|confirmed',
        ]);

        if ($validator->fails()){

            return $this->ReturnFailed(1,$validator->errors());
        }

        $user = AdminUser::find($this->auth()->user()->id);

        $user->password = Hash::make($post['password']);
        $user->save();

        return $this->ReturnSuccessInfo();
    }
}
